package com.lazyframework.generator.mapper;

import com.lazyframework.generator.model.Column;
import com.lazyframework.generator.model.Schema;
import com.lazyframework.generator.model.Table;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * describes
 * @author  lazy
 */
@Mapper
public interface SchemaMapper {

    @Select("SELECT SCHEMA_NAME as name FROM SCHEMATA")
    List<Schema> schemas();

    @Select("SELECT TABLE_NAME as name, TABLE_COMMENT as comment FROM TABLES WHERE TABLES.TABLE_SCHEMA = #{schema}")
    List<Table> tables(String schema);

    @Select("SELECT\n" +
            "   COLUMN_NAME as name, COLUMN_COMMENT as comment, DATA_TYPE as type\n" +
            "FROM\n" +
            "   COLUMNS\n" +
            "WHERE\n" +
            "   COLUMNS.TABLE_NAME = #{table}\n AND COLUMNS.TABLE_SCHEMA = #{schema}" +
            "AND\n " +
            "   COLUMNS.COLUMN_NAME NOT IN ('update_time', 'create_time')"
    )
    List<Column> columns(@Param("table") String table, @Param("schema") String schema);

    @Select("SELECT\n " +
            "   TABLE_NAME as name, TABLE_COMMENT as comment\n " +
            "FROM\n " +
            "   TABLES\n " +
            "WHERE\n " +
            "   TABLES.TABLE_NAME = #{table} and TABLES.TABLE_SCHEMA = #{schema}"
    )
    Table findTable(@Param("table") String table, @Param("schema") String schema);

}
